home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The X-Philes (2nd Revision)
/
The X-Philes Number 1 (1995).iso
/
xphiles
/
hp48_1
/
grob_to.bit
< prev
next >
Wrap
Text File
|
1995-03-23
|
5KB
|
189 lines
From arne%SFD.UIT.NO@VM1.NoDak.EDU Sun Apr 22 20:02:03 1990
Date: Sun, 22 Apr 90 19:59:59 met
Reply-To: HP-48 - HP-48sx Hand Held System <HP-48@VM1.NoDak.EDU>
Sender: HP-48 - HP-48sx Hand Held System <HP-48@VM1.NoDak.EDU>
From: Arne Helme <arne%SFD.UIT.NO@VM1.NoDak.EDU>
To: Multiple recipients of list HP-48 <HP-48@NDSUVM1>
Hello HP48sx users !
This is a little hack I wrote to convert GROB's into portable bitmaps.
It takes input from stdin or a file and writes to stdout (UNIX !).
You can convert a portable bitmap into many other interesting formats
using the pbmplus package (referenced below). Please report bugs or
improvements.
-- Arne Helme (E-mail : arne@sfd.uit.no)
/* -*-C-*-
********************************************************************************
*
* File: grob2pbm.c
* RCS: $Header: $
* Description: HP48sx ASCII GROB format to PBM (portable bitmap)
* Author: Arne Helme
* Created: Fri Mar 30 09:12:31 1990
* Modified: Sun Apr 22 15:20:34 1990 (Arne Helme) arne@dstud2
* Language: C
* Package: N/A
* Status: Experimental (Do Not Distribute)
*
* (c) Copyright 1990, arne@sfd.uit.no, all rights reserved.
*
********************************************************************************
*/
/*
DESCRIPTION
This program converts a HP48sx ASCII GROB file into a portable bitmap.
Export your HP48sx GROBs to your computer, convert them to your favorite
format and use them in your documents.
The portable bitmap (PBM) is a interchange format in the Extended Portable
Bitmap Toolkit written by Mr. Jef Poskanzer and the
PBM software is public domain.
STATUS
A two hour's hack.
KNOWN BUGS
Wont take binary GROBS yet. Only tested on UNIX systemV machines.
NOTE
A public domain PBM package is available via anonymous ftp to
sfd.uit.no [128.39.60.50] (pub/X11/pbmplus.tar.Z).
*/
#include <stdio.h>
#define GROB_MAGIC "GROB"
#define PBM_MAGIC "P1"
#define SPACE ' '
#define NEWLINE '\n'
#define ZERO '0'
#define ONE '1'
#define TRANSLATE '%'
#define HP48sxHEADERSIZE 25
#define MAXLINE 64
#define ASCIISIZE (int)((width / 4) + 1) * height
void convert (nibble, num)
int *nibble, num;
{
int i;
for (i = 0; i < num; i++) {
*nibble & 1 ? fputc (ONE, stdout) : fputc (ZERO, stdout);
fputc (SPACE, stdout);
*nibble >>= 1;
}
}
void main(argc, argv)
int argc;
char *argv[];
{
FILE *fp; /* File pointer for reading GROB data */
char header[HP48sxHEADERSIZE]; /* GROB header */
int ch; /* Temporary character */
int width; /* GROB width */
int height; /* GROB height */
int nibble1, nibble2; /* Each containing a 4 bit nibble */
int i, count, line = 0; /* Loop and control registers */
/* Parse command line */
switch (argc) {
case 1:
/* We'll have to read from stdin */
fp = stdin;
break;
case 2:
/* Program started with one argument, assume it is a filename */
fp = fopen (argv[1], "r");
if (fp == (FILE *)NULL) {
fprintf (stderr, "%s : unable to read file %s\n",
argv[0], argv[1]);
exit (1);
}
break;
default :
/* Unable to parse command line */
fprintf (stderr, "Usage : %s <GROBfile>\n", argv[0]);
exit (1);
}
/* Skip HP48sx translation information if any */
if ((ch = fgetc(fp)) == EOF) {
fprintf (stderr, "%s : unexpected end-of-file found\n",
argv[0]);
exit (1);
}
if (ch == TRANSLATE) {
if (fgets (header, HP48sxHEADERSIZE, fp) == NULL) {
fprintf (stderr, "%s : error reading HP48sx header\n", argv[0]);
exit (1);
}
} else
ungetc (ch, fp);
if (fscanf (fp, "%s", header) != 1) {
fprintf (stderr, "%s : error reading GROB magic number\n", argv[0]);
exit (1);
}
if (strcmp(header, GROB_MAGIC)) {
fprintf (stderr, "%s : bad magic number\n", argv[0]);
exit (1);
}
if (fscanf (fp, "%d %d", &width, &height) != 2) {
fprintf (stderr, "%s : unable to read GROB size\n", argv[0]);
exit (1);
}
/* Print new magic number and sizes to stdout */
printf ("%s\n%d %d\n", PBM_MAGIC, width, height);
/* Skip blank if any*/
if ((ch =fgetc(fp)) != ' ')
ungetc (ch, fp);
/* Convert GROB data to PBM data */
for (i=0; i < height; i++) {
count = width;
do {
/* Scan in two four-bit nibbles (one byte) */
fscanf (fp, "%1X%1X", &nibble1, &nibble2);
/* Check if we are in the last bits of current line */
if (count >= 8) {
/* Convert 1st 4 bit nibble */
convert (&nibble1, 4);
/* Convert 2nd 4 bit nibble */
convert (&nibble2, 4);
} else { /* Last bits, watch out for padded bits */
/* check if we have a complete 1st four bit nibble */
if (count >= 4) {
convert (&nibble1, 4);
convert (&nibble2, count - 4);
} else
convert (&nibble1, count);
}
/* PBM wont't take lines longer than 70 chars, insert newline */
line += 16;
if (!(line % MAXLINE))
fputc (NEWLINE, stdout);
} while ((count -= 8) > 0);
}
fclose (stdout);
}